﻿using CommonBase;
using CommonClass;
using EnvControlLib;
using FileLib;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Regions;
using SurveillanceLib;
using SurveillancePro.Base.ViewModels;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Forms;

namespace ToolkitMgtModule.ViewModels
{
    /// <summary>
    /// 配置管理
    /// </summary>
    public class ViewModel_UserConfig : WorkspaceViewModel
    {
        #region 变量

        private DelegateCommand _cmdApply, _cmdCancel, _cmdReset;
        private DelegateCommand<string> _cmdOpenDialogue;

        #endregion

        #region 构造函数

        public ViewModel_UserConfig(IRegionManager regionManager)
        {
            if (regionManager == null)
                return;

            base.BaseRegionManager = regionManager;
            base.ViewName = SurveillancePro.Base.Common.ViewNames.ToolkitMgtModule_UserConfig;
            base.HeaderTitle = "配置管理";
            base.HeaderImageSource = string.Format(ConstantString.IMAGE_URL, "Question");
            base.IsShowCloseButton = Visibility.Visible;

            _downloadVideoPath = SurveillanceUtilities.DownloadVideoPath;
            _recordVideoPath = SurveillanceUtilities.RecordVideoPath;
            _convertVideoPath = SurveillanceUtilities.ConvertVideoPath;
            _screenShotPath = SurveillanceUtilities.ScreenShotPath;
        }

        #endregion

        #region Base Class Overrides

        protected override void OnDispose()
        {
        }

        #endregion

        #region 属性

        private string _downloadVideoPath;
        /// <summary>
        /// 录像文件下载路径
        /// </summary>
        public string DownloadVideoPath
        {
            get { return _downloadVideoPath; }
            set
            {
                if (_downloadVideoPath == value) return;
                _downloadVideoPath = value;
                base.OnPropertyChanged("DownloadVideoPath");
            }
        }

        private string _recordVideoPath;
        /// <summary>
        /// 录像路径
        /// </summary>
        public string RecordVideoPath
        {
            get { return _recordVideoPath; }
            set
            {
                if (_recordVideoPath == value) return;
                _recordVideoPath = value;
                base.OnPropertyChanged("RecordVideoPath");
            }
        }

        private string _convertVideoPath;
        /// <summary>
        /// 转换路径
        /// </summary>
        public string ConvertVideoPath
        {
            get { return _convertVideoPath; }
            set
            {
                if (_convertVideoPath == value) return;
                _convertVideoPath = value;
                base.OnPropertyChanged("ConvertVideoPath");
            }
        }

        private string _screenShotPath;
        /// <summary>
        /// 截图路径
        /// </summary>
        public string ScreenShotPath
        {
            get { return _screenShotPath; }
            set
            {
                if (_screenShotPath == value) return;
                _screenShotPath = value;
                base.OnPropertyChanged("ScreenShotPath");
            }
        }


        /// <summary>
        /// 浏览文件夹
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand<string> Cmd_OpenDialogue
        {
            get
            {
                if (_cmdOpenDialogue == null)
                {
                    _cmdOpenDialogue = new DelegateCommand<string>(_openDialogue, _canOpenDialogue);
                }
                return _cmdOpenDialogue;
            }
        }

        /// <summary>
        /// 确认
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand Cmd_Apply
        {
            get
            {
                if (_cmdApply == null)
                {
                    _cmdApply = new DelegateCommand(_apply, _canApply);
                }
                return _cmdApply;
            }
        }

        /// <summary>
        /// 取消
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand Cmd_Cancel
        {
            get
            {
                if (_cmdCancel == null)
                {
                    _cmdCancel = new DelegateCommand(_cancel, _canCancel);
                }
                return _cmdCancel;
            }
        }

        /// <summary>
        /// 重置
        /// </summary>
        [Display(GroupName = "DelegateCommand")]
        public DelegateCommand Cmd_Reset
        {
            get
            {
                if (_cmdReset == null)
                {
                    _cmdReset = new DelegateCommand(_reset, _canReset);
                }
                return _cmdReset;
            }
        }

        #endregion

        #region 方法

        #region DelegateCommand

        private bool _canApply()
        {
            return true;
        }

        private bool _canCancel()
        {
            return true;
        }

        private bool _canReset()
        {
            return true;
        }

        private bool _canOpenDialogue(string itemType)
        {
            return true;
        }


        /// <summary>
        /// 确认
        /// </summary>
        private void _apply()
        {
            // 重新写入全局静态变量保存
            SurveillanceUtilities.DownloadVideoPath = _downloadVideoPath;
            SurveillanceUtilities.RecordVideoPath = _recordVideoPath;
            SurveillanceUtilities.ConvertVideoPath = _convertVideoPath;
            SurveillanceUtilities.ScreenShotPath = _screenShotPath;

            // 修改 ini 文件
            INIOperation iniOperation = new INIOperation();
            string iniPath = string.Format(@"{0}\{1}\{2}.ini", Environment.CurrentDirectory,
                "DataBase", Utilities.DatabaseName);
            iniOperation.WriteValue(SurveillanceUtilities.INI_SECTIONNAME_USERCONFIG,
                SurveillanceUtilities.INI_KEYNAME_USERCONFIG_DOWNLOADVIDEOPATH, _downloadVideoPath, iniPath);
            iniOperation.WriteValue(SurveillanceUtilities.INI_SECTIONNAME_USERCONFIG,
                SurveillanceUtilities.INI_KEYNAME_USERCONFIG_RECORDVIDEOPATH, _recordVideoPath, iniPath);
            iniOperation.WriteValue(SurveillanceUtilities.INI_SECTIONNAME_USERCONFIG,
                SurveillanceUtilities.INI_KEYNAME_USERCONFIG_CONVERTVIDEOPATH, _convertVideoPath, iniPath);
            iniOperation.WriteValue(SurveillanceUtilities.INI_SECTIONNAME_USERCONFIG,
                SurveillanceUtilities.INI_KEYNAME_USERCONFIG_SCREENSHOTPATH, _screenShotPath, iniPath);

            AutoCloseNoticeWindow.Show("保存成功，重启程序后生效！", false);
            this.CloseCommand.Execute(null);
        }

        /// <summary>
        /// 取消
        /// </summary>
        private void _cancel()
        {
            DownloadVideoPath = SurveillanceUtilities.DownloadVideoPath;
            RecordVideoPath = SurveillanceUtilities.RecordVideoPath;
            ConvertVideoPath = SurveillanceUtilities.ConvertVideoPath;
            ScreenShotPath = SurveillanceUtilities.ScreenShotPath;

            this.CloseCommand.Execute(null);
        }

        /// <summary>
        /// 重置
        /// </summary>
        private void _reset()
        {
            DownloadVideoPath = SurveillanceUtilities.USERCONFIG_DOWNLOADVIDEOPATH_DEFAULT;
            RecordVideoPath = SurveillanceUtilities.USERCONFIG_RECORDVIDEOPATH_DEFAULT;
            ConvertVideoPath = SurveillanceUtilities.USERCONFIG_CONVERTVIDEOPATH_DEFAULT;
            ScreenShotPath = SurveillanceUtilities.USERCONFIG_SCREENSHOTPATH_DEFAULT;
        }

        /// <summary>
        /// 浏览文件夹
        /// </summary>
        /// <param name="itemType"></param>
        private void _openDialogue(string itemType)
        {
            FolderBrowserDialog folder = new FolderBrowserDialog()
            {
                Description = "请选择文件夹路径",
            };

            if (folder.ShowDialog() == DialogResult.OK)
            {
                switch (itemType)
                {
                    case "DownloadVideo":
                        DownloadVideoPath = folder.SelectedPath.Trim();
                        break;
                    case "RecordVideo":
                        RecordVideoPath = folder.SelectedPath.Trim();
                        break;
                    case "ConvertVideo":
                        ConvertVideoPath = folder.SelectedPath.Trim();
                        break;
                    case "ScreenShot":
                        ScreenShotPath = folder.SelectedPath.Trim();
                        break;


                    default:
                        break;
                }
            }
        }

        #endregion

        #endregion
    }
}
